using Hospital.IRepository;
using Hospital.IService;
using Hospital.Model.entity;
using Hospital.Utility._MD5;
using Hospital.Utility.ApiResult;

namespace Hospital.Service
{
    public class PatientInfoService : BaseService<PatientInfo>, IPatientInfoService
    {
        private readonly IPatientInfoRepository _IPatientInfoRepository;

        public PatientInfoService(IPatientInfoRepository iPatientInfoRepository)
        {
            this._IPatientInfoRepository = iPatientInfoRepository;
            base._iBaseRepository = _IPatientInfoRepository;
        }

        public async Task<PatientInfo> LoginAsync(string phone, string password)
        {
            var patientInfo = await _IPatientInfoRepository.QueryAsync(p => p.Phone == phone && p.Password == password);
            if (patientInfo.Count == 0)
            {
                throw new Exception("用户名或密码错误");
            }
            return patientInfo[0];
        }

        public async Task<ApiResult> RegisterAsync(PatientInfo newPatientInfo)
        {
            var patientInfo = await _IPatientInfoRepository.QueryAsync(p => p.IdCard == newPatientInfo.IdCard);
            if (patientInfo.Count > 0)
            {
                return ApiResultHelper.Error(500, "身份证已经注册");
            }
            //加密密码
            newPatientInfo.Password = MD5Helper.MD5Encrypt32(newPatientInfo.Password);
            await _IPatientInfoRepository.CreateAsync(newPatientInfo);
            return ApiResultHelper.Success("注册成功");
        }
    }
}
